const mongoose = require('mongoose')
const mongoosePaginate = require('mongoose-paginate')
const path = require('path')

module.exports = function (db) {
    return db.model(path.basename(__filename, path.extname(__filename)), new mongoose.Schema(
        {
            id: {
                type: String,
                unique: true,
                required: true,
                index: true,
            },

            unit_id: {   // 以单位为基本配置
                type: String,
                index: true,
            },

            type_code: { // 类型 事件，任务
                type: String,
                index: true,
            },

            type_id: { // 事件或者任务的id
                type: String,
                index: true,
            },

            device_id: {
                type: String,
                index: true,
            },

            // 消息内容 类型 1=文本，2=JSON,3=图片，4=声音，5=视频, 6=设备事件
            content_type: {
                type: Number,
                index: true,
                default: 1,
            },

            content: mongoose.Schema.Types.Mixed,

            //创建时间
            created_at: {
                type: Date,
                index: true,
                default: Date.now
            },

            updated_at: {
                type: Date,
                default: Date.now
            },
        },
        {
            collection: path.basename(__filename, path.extname(__filename)),
            timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' }
        }).plugin(mongoosePaginate)
    )
}
